# - Locate NeL 3D library
# This module defines
#  NEL3D_LIBRARY, the library to link against
#  NEL_FOUND, if false, do not try to link to NEL3D
#  NEL_INCLUDE_DIR, where to find headers.

IF(NELNS_NAMING_SERVICE_BIN AND NELNS_COMMON_CFG)
  # in cache already
  SET(NELNS_FIND_QUIETLY TRUE)
ENDIF(NELNS_NAMING_SERVICE_BIN AND NELNS_COMMON_CFG)

# Assume we find NeL and correct it along the way.
SET(NELNS_FOUND "YES")

# determine the components to retrieve.
IF(NOT NeLNS_FIND_COMPONENTS)
  # We must always have naming_service.
  SET(NeLNS_FIND_COMPONENTS naming_service)
ELSE(NOT NeLNS_FIND_COMPONENTS)
  # Make sure we have naming_service in the list.
  LIST(FIND NeLNS_FIND_COMPONENTS naming_service TMP_FIND_NELNS)
  IF(TMP_FIND_NELNS EQUAL -1)
    LIST(APPEND NeLNS_FIND_COMPONENTS naming_service)
  ENDIF(TMP_FIND_NELNS EQUAL -1)
ENDIF(NOT NeLNS_FIND_COMPONENTS)  

FIND_FILE(NELNS_COMMON_CFG
				NAME NAME common.cfg
				PATHS
				[HKEY_LOCAL_MACHINE\\SOFTWARE\\NeL\\NeLNS;]/etc/nel/nelns
				$ENV{ProgramFiles}/NeLNS/etc/nel/nelns
				$ENV{NELNS_DIR}/etc/nel/nelns)

# Make sure we found the include files.
IF(NOT NELNS_COMMON_CFG)
  SET(NEL_FOUND "NO")
ENDIF(NOT NELNS_COMMON_CFG)

# A utility macro to wrap NeL finds...
MACRO(FIND_NELNS_PROGRAM NELNSPROGRAM NELNSBINNAME)   
  FIND_PROGRAM(NELNS_${NELNSPROGRAM}_BIN
				NAME ${NELNSBINNAME}
				PATHS 
				[HKEY_LOCAL_MACHINE\\SOFTWARE\\NeL\\NeLNS;]/sbin
				[HKEY_LOCAL_MACHINE\\SOFTWARE\\NeL\\NeLNS;]/bin
				$ENV{ProgramFiles}/NeLNS/sbin
				$ENV{ProgramFiles}/NeLNS/bin
				$ENV{NELNS_DIR}/sbin
				$ENV{NELNS_DIR}/bin)			
  FIND_FILE(NELNS_${NELNSPROGRAM}_CFG
				NAME NAME ${NELNSBINNAME}.cfg
				PATHS
				[HKEY_LOCAL_MACHINE\\SOFTWARE\\NeL\\NeL;]/etc/nel/nelns
				$ENV{ProgramFiles}/NeLNS/etc/nel/nelns
				$ENV{NELNS_DIR}/etc/nel/nelns)
ENDMACRO(FIND_NELNS_PROGRAM NELNSPROGRAM) 


# Find the library for each required library.
FOREACH(NL_F_COMPONENT ${NeLNS_FIND_COMPONENTS})

  # Check for NeL Naming Service
  IF(NL_F_COMPONENT STREQUAL "naming_service")
	FIND_NELNS_PROGRAM(NAMING_SERVICE naming_service)
    
    IF(NOT NELNS_NAMING_SERVICE_BIN OR NOT NELNS_NAMING_SERVICE_CFG)
      SET(NELNS_FOUND "NO")
    ENDIF(NOT NELNS_NAMING_SERVICE_BIN OR NOT NELNS_NAMING_SERVICE_CFG)
  
  # Check for NeL Login Service
  ELSEIF(NL_F_COMPONENT STREQUAL "login_service")
	FIND_NELNS_PROGRAM(LOGIN_SERVICE login_service)
    
    IF(NOT NELNS_LOGIN_SERVICE_BIN OR NOT NELNS_LOGIN_SERVICE_CFG)
      SET(NELNS_FOUND "NO")
    ENDIF(NOT NELNS_LOGIN_SERVICE_BIN OR NOT NELNS_LOGIN_SERVICE_CFG)
    
  # Check for NeL Welcome Service
  ELSEIF(NL_F_COMPONENT STREQUAL "welcome_service")
	FIND_NELNS_PROGRAM(WELCOME_SERVICE welcome_service)
    
    IF(NOT NELNS_WELCOME_SERVICE_BIN OR NOT NELNS_WELCOME_SERVICE_CFG)
      SET(NELNS_FOUND "NO")
    ENDIF(NOT NELNS_WELCOME_SERVICE_BIN OR NOT NELNS_WELCOME_SERVICE_CFG)
    
  # Check for NeL Admin Service
  ELSEIF(NL_F_COMPONENT STREQUAL "admin_service")
	FIND_NELNS_PROGRAM(ADMIN_SERVICE admin_service)
    
    IF(NOT NELNS_ADMIN_SERVICE_BIN OR NOT NELNS_ADMIN_SERVICE_CFG)
      SET(NELNS_FOUND "NO")
    ENDIF(NOT NELNS_ADMIN_SERVICE_BIN OR NOT NELNS_ADMIN_SERVICE_CFG)
    
  # Check for NeL Admin Executor Service
  ELSEIF(NL_F_COMPONENT STREQUAL "admin_executor_service")
	FIND_NELNS_PROGRAM(ADMIN_EXECUTOR_SERVICE admin_executor_service)
    
    IF(NOT NELNS_ADMIN_EXECUTOR_SERVICE_BIN OR NOT NELNS_ADMIN_EXECUTOR_SERVICE_CFG)
      SET(NELNS_FOUND "NO")
    ENDIF(NOT NELNS_ADMIN_EXECUTOR_SERVICE_BIN OR NOT NELNS_ADMIN_EXECUTOR_SERVICE_CFG)
    
  # Check for NeL Qt Launcher
  ELSEIF(NL_F_COMPONENT STREQUAL "nel_launcher_qt")
	FIND_NELNS_PROGRAM(NEL_LAUNCHER nel_launcher_qt)
    
    IF(NOT NELNS_NEL_LAUNCHER_BIN)
      SET(NELNS_FOUND "NO")
    ENDIF(NOT NELNS_NEL_LAUNCHER_BIN)
      
  # Output an error message if an unknown component is requested.
  ELSE(NL_F_COMPONENT STREQUAL "naming_service")
    MESSAGE(ERROR " Unknown component ${NL_F_COMPONENT}!!")
    SET(NELNS_FOUND "NO")
  ENDIF(NL_F_COMPONENT STREQUAL "naming_service")
ENDFOREACH(NL_F_COMPONENT ${NeLNS_FIND_COMPONENTS})

IF(NELNS_FOUND STREQUAL "YES")
  IF(NOT NELNS_FIND_QUIETLY)
    MESSAGE(STATUS "Found NeLNS: ${NELNS_COMMON_CFG}")
  ENDIF(NOT NELNS_FIND_QUIETLY)
ELSE(NELNS_FOUND STREQUAL "YES")
  IF(NOT NELNS_FIND_QUIETLY)
    MESSAGE(STATUS "Warning: Unable to find NeLNS!")
  ENDIF(NOT NELNS_FIND_QUIETLY)
ENDIF(NELNS_FOUND STREQUAL "YES")
